# -*- coding: utf-8 -*-
"""
Created on Mon May 04 10:53:51 2015

@author: zhaoxp
"""

import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.image import MIMEImage

"""
sender: sender's mail address
receivers: receivers' addresses seperated by semicolon(;)
title: mail title
message: mail content
mailServer: SMTP server
"""
def sendPlainTextMail(sender,receivers,title,message,mailServer):
    try:
        content="From: "+sender+"\n"+"To: "+receivers+"\n"+"Subject: "+title+"\n\n"+message+"\n"
        smtpObj=smtplib.SMTP(mailServer)
        #smtpObj=smtplib.SMTP()
        #smtpObj.connect(mailServer)
        #smtpObj.login("username","password")
        smtpObj.sendmail(sender,receivers,content)
        smtpObj.close()
    except SMTPException:
        print("Unable to send mail!")

"""
sender: sender's mail address
receivers: receivers' addresses seperated by semicolon(;)
title: mail title
message: mail content wiht html format, for example: <html><h1>Hello HTML mail!</h1></html>
mailServer: SMTP server
"""
def sendHtmlMail(sender,receivers,title,htmlMessage,mailServer):
    try:
        #'<html><h1>Hello HTML mail!</h1></html>'
        msg=MIMEText(htmlMessage,_subtype='html',_charset='utf-8')
        msg['Subject']=title
        msg['From']=sender
        msg['To']=receivers
        content="From: "+sender+"\n"+"To: "+receivers+"\n"+"Subject: "+title+"\n\n"+message+"\n"
        smtpObj=smtplib.SMTP(mailServer)
        #smtpObj=smtplib.SMTP()
        #smtpObj.connect(mailServer)
        #smtpObj.login("username","password")
        smtpObj.sendmail(sender,receivers,msg.as_string())
        smtpObj.close()
    except SMTPException:
        print("Unable to send mail!")

"""
sender: sender's mail address
receivers: receivers' addresses seperated by semicolon(;)
title: mail title
message: mail content 
picPath: pic full path
mailServer: SMTP server
"""
def sendHtmlMail_WithPic(sender,receivers,title,message,picPath,mailServer):
    try:
        msgRoot=MIMEMultipart('Related')
        msgRoot['Subject']=title
        msgRoot['From']=sender
        msgRoot['To']=receivers
        msgText=MIMEText(message+'<br>This is an image:<img src="cid:image1"> <br>End',_subtype='html',_charset='utf-8')
        msgRoot.attach(msgText)
        fp=open(picPath,'rb')
        msgImage=MIMEImage(fp.read())
        fp.close()
        msgImage.add_header('Content-ID','<image1>')
        msgRoot.attach(msgImage)
        smtpObj=smtplib.SMTP(mailServer)
        smtpObj.sendmail(sender,receivers,msgRoot.as_string())
        smtpObj.close()
    except SMTPException:
        print("Unable to send mail!")
    except Exception:
        print("error...................")

"""
sender: sender's mail address
receivers: receivers' addresses seperated by semicolon(;)
title: mail title
message: mail content 
filePath: file full path
mailServer: SMTP server
"""        
def sendMail_WithAttachement(sender,receivers,title,message,filePath,mailServer):
    try:
        msgRoot=MIMEMultipart('related')
        msgRoot['Subject']=title
        msgRoot['From:']=sender
        msgRoot['To:']=receivers
        f=open(filePath,'rb')
        att=MIMEText(f.read(),'base64','utf-8')
        att["Content-Type"]='application/octet-stream'
        att["Content-disposition"]='attachment; filename="attfile.txt"'
        #f.close()
        msgRoot.attach(att)
        smtpObj=smtplib.SMTP(mailServer)
        smtpObj.sendmail(sender,receivers,msgRoot.as_string())
        smtpObj.close()
    except SMTPException:
        print("Unable to send mail!")
#######################################################test#######################################################
sender="zhaoxp2@lenovo.com"
receivers=["zhaoxp2@lenovo.com","zhaoxp2@lenovo.com"]
#receivers=["zhaoxp2@lenovo.com","liuxt2@lenovo.com","zhangll9@lenovo.com"]
message="""From: zhaoxp2@lenovo.com
To: zhaoxp2@lenovo.com;liuxt2@lenovo.com;zhangll9@lenovo.com
Subject: SMTP e-mail test

This is a test e-mail message.
"""
"""
try:
    smtpObj=smtplib.SMTP('smtpinternal.lenovo.com')
    smtpObj.sendmail(sender,receivers,message)
    print("Successfully sent mail!")
except SMTPException:
    print("Unable to send mail!")

"""

#######################################################test#######################################################

print("Mail test ...")

#sendPlainTextMail("zhaoxp2@lenovo.com","zhaoxp2@lenovo.com;liuxt2@lenovo.com;zhangll9@lenovo.com","test mail title","test mail content","smtpinternal.lenovo.com")
#sendHtmlMail("zhaoxp2@lenovo.com","zhaoxp2@lenovo.com;liuxt2@lenovo.com;zhangll9@lenovo.com","test mail title","<html><h1>Hello HTML mail!</h1></html>","smtpinternal.lenovo.com")
#sendHtmlMail_WithPic("zhaoxp2@lenovo.com","zhaoxp2@lenovo.com;liuxt2@lenovo.com;zhangll9@lenovo.com","test mail title","<html><h1>Hello HTML mail!</h1></html>","C:\\app\\Apache2.2\\icons\\apache_pb.gif","smtpinternal.lenovo.com")
sendMail_WithAttachement("zhaoxp2@lenovo.com","zhaoxp2@lenovo.com;liuxt2@lenovo.com;zhangll9@lenovo.com","test mail title","<html><h1>Hello HTML mail!</h1></html>","C:\\app\\Apache2.2\\icons\\apache_pb.gif","smtpinternal.lenovo.com")


print("end of python mail test")